package com.globallogic.learning.kozius.entity;

import com.globallogic.learning.kozius.actor.Student;
import com.globallogic.learning.kozius.actor.Teacher;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Class describes a seminar entity
 * <p/>
 * Created by sergii.koziumynskyi on 2/4/14.
 */
public final class Seminar {
    public Seminar(String topic, int yearOfSeminar, int monthOfSeminar, int dayOfSeminar,
                   Teacher lecturer, Auditorium auditorium) {
        this.topic = topic;
        GregorianCalendar calendar = new GregorianCalendar(yearOfSeminar, monthOfSeminar - 1, dayOfSeminar);
        this.dateOfSeminar = calendar.getTime();
        this.lecturer = lecturer;
        this.auditorium = auditorium;
    }

    public Seminar(String topic, int yearOfSeminar, int monthOfSeminar, int dayOfSeminar) {
        this.topic = topic;
        GregorianCalendar calendar = new GregorianCalendar(yearOfSeminar, monthOfSeminar - 1, dayOfSeminar);
        this.dateOfSeminar = calendar.getTime();
    }

    public Seminar(String topic) {
        this.topic = topic;
    }

    public Seminar() {
    }

    public int getId() {
        return id;
    }

    public String getTopic() {
        return topic;
    }

    public Date getDateOfSeminar() {
        return dateOfSeminar;
    }

    public Teacher getLecturer() {
        return lecturer;
    }

    public List<Student> getStudents() {
        return students;
    }

    public Auditorium getAuditorium() {
        return auditorium;
    }

    public void setTopic(String topic) {
        if (topic.length() < 10) this.topic = topic;
    }

    public void setDateOfSeminar(int yearOfSeminar, int monthOfSeminar, int dayOfSeminar) {
        GregorianCalendar calendar = new GregorianCalendar(yearOfSeminar, monthOfSeminar - 1, dayOfSeminar);
        this.dateOfSeminar = calendar.getTime();
    }

    public void setLecturer(Teacher lecturer) {
        this.lecturer = lecturer;
    }

    public void setAuditorium(Auditorium auditorium) {
        this.auditorium = auditorium;
    }

    /**
     * Methods tries to add students to a list of students registered
     *
     * @return 1 - if operation is successful;
     * 2 - failed: auditorium is not set yet;
     * 3 - failed: capacity of auditory is exhausted; reduce number of students or change auditorium;
     */
    public int setStudents(List<Student> students) {
        int result;
        if (auditorium == null) {
            result = 2;
        } else if (auditorium.getAuditoriumCapacity() < students.size()) {
            result = 3;
        } else {
            this.students = students;
            result = 1;
        }
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Seminar seminar = (Seminar) o;

        if (id != seminar.id) return false;
        if (auditorium != null ? !auditorium.equals(seminar.auditorium) : seminar.auditorium != null) return false;
        if (dateOfSeminar != null ? !dateOfSeminar.equals(seminar.dateOfSeminar) : seminar.dateOfSeminar != null)
            return false;
        if (lecturer != null ? !lecturer.equals(seminar.lecturer) : seminar.lecturer != null) return false;
        if (students != null ? !students.equals(seminar.students) : seminar.students != null) return false;
        if (topic != null ? !topic.equals(seminar.topic) : seminar.topic != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = topic != null ? topic.hashCode() : 0;
        result = 31 * result + (dateOfSeminar != null ? dateOfSeminar.hashCode() : 0);
        result = 31 * result + (lecturer != null ? lecturer.hashCode() : 0);
        result = 31 * result + (students != null ? students.hashCode() : 0);
        result = 31 * result + (auditorium != null ? auditorium.hashCode() : 0);
        result = 31 * result + id;
        return result;
    }

    @Override
    public String toString() {
        return "Seminar{" +
                "topic='" + topic + '\'' +
                ", dateOfSeminar=" + dateOfSeminar +
                ", lecturer=" + lecturer +
                ", students=" + students +
                ", auditorium=" + auditorium +
                ", id=" + id +
                '}';
    }

    private String topic = "";
    private Date dateOfSeminar;
    private Teacher lecturer;
    private List<Student> students = new ArrayList<Student>();
    private Auditorium auditorium;

    private int id;
    private static int nextId = 500000000; // 5 prefix will show that it is a seminar

    {
        id = nextId;
        nextId++;
    }
}
